package org.geektime.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.*;

@EnableConfigurationProperties(DataSourceProperties.class)
@Component
public class JdbcDataManageService {
  @Autowired DataSourceProperties dataSourceProperties;

  @PostConstruct
  public void doOnce() {
    try {
      Class.forName(dataSourceProperties.getDriverClassName());
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    try {
      connection =
          DriverManager.getConnection(
              dataSourceProperties.getUrl(),
              dataSourceProperties.getUsername(),
              dataSourceProperties.getPassword());
      statement = connection.createStatement();
      rs = statement.executeQuery("select classcode,\"TYPE\" from s_risklist limit 100");
      while (rs.next()) {
        System.out.printf("%s ---- %s \r\n", rs.getString("classcode"), rs.getString("type"));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        rs.close();
        statement.close();
        connection.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}
